
//采购订单
app.controller('linePurchaseOrderController', ['$scope','linePurchaseService','ajax','$state','$filter','Flash',function ($scope,linePurchaseService,ajax,$state,$filter,Flash) {

    list(1);
    //获取分页
    function list(page){
        linePurchaseService.list({
            currentPage: page,
            pageSize: 10
        }).then(function (result) {
            //console.log(result);
            $scope.list=result.resultData;
            $scope.page=result;
            $scope.queryData.currentPage=result.currentPage;
            $scope.queryData.totalSize=result.totalSize;
            $scope.queryData.totalPageSize=result.totalPageSize;
        });
    };
    //分页
    $scope.pageChanged = function(e) {
        list(e);
    };

    $scope.data= {
        isShowDetail : false,//是否显示详细
        purchaseOrderId : "",
        purchaseOrderStatus :'',
        purchaseOrderDetailId:'',
        number : '',
        curPrice : "",
        outPrice : ''
    };

    $scope.queryData = {
        ctime_tstart:'',
        ctime_tend: '',
        purchaseOrderId: '',
        currentPage: '',
        pageSize: 10,
        totalSize:'',
        totalPageSize:''
    };
    $scope.doQuery = function (purchaseOrderId) {
        var ctime_tstart=$filter('date')($scope.queryData.ctime_tstart,'yyyy-MM-dd');
        $scope.queryData.ctime_tstart=ctime_tstart;
        var ctime_tend=$filter('date')($scope.queryData.ctime_tend,'yyyy-MM-dd');
        $scope.queryData.ctime_tend=ctime_tend;
        $scope.queryData.purchaseOrderId=purchaseOrderId;
        ajax.get({
            url: "back/purchaseOrder/page",
            data: $scope.queryData
        }).then(function (result) {
            //console.log(result);
            $scope.list = result.resultData;
            $scope.page=result;
        });
    };
    //重置按钮
    $scope.reset=function(){
        $scope.queryData = {};
    }
    //查询采购单详情列表
    $scope.queryPurchaseDetail = function(purchaseOrderId,purchaseOrderStatus){
        ajax.get({
            url : "back/linePurchaseOrder/pageDetail",
            data : {
                purchaseOrderId : purchaseOrderId
            }
        }).then(function(result){
            $scope.data.isShowDetail = true;
            $scope.data.purchaseOrderStatus=purchaseOrderStatus;
            $scope.purchaseOrderrDetailList = result.resultData;
        });
    }

    //修改价格
    $scope.curPriceUpdate = function(purchaseOrderDetailId,curPrice,outPrice){
        ajax.post({
            url : "back/linePurchaseOrder/updateDetail",
            data : {
                purchaseOrderDetailId : purchaseOrderDetailId,
                curPrice : curPrice,
                outPrice : outPrice
            }
        }).then(function(result){
            Flash.create('success', '修改成功', 'custom-class');
        });
    }
    //修改售价

    //采购完成
    $scope.procurementCompletion = function(purchaseOrderId){
        ajax.get({
            url : "/back/purchaseOrder/purchaseFinish",
            data : {
                purchaseOrderId : purchaseOrderId
            }
        }).then(function(result){
            $scope.data.isShowDetail = false;
            alert('采购完成成功！');
            $state.go('admin.linePurchaseOrder',null,{
                reload:true
            });
        });
    }
}]).filter('status', function(){
        return function(item){
            var arr = ['未采购','采购中','采购完成'];
            return arr[item];
        }
    });